﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Net;
using System.Net.Security;
using System.Net.Mail;

namespace DbTask
{
    public static class EmailSender
    {
        private static readonly string from = ConfigurationManager.AppSettings["SmtpFrom"];
        private static readonly string host = ConfigurationManager.AppSettings["SmtpHost"];
        private static readonly string port = ConfigurationManager.AppSettings["SmtpPort"];
        private static readonly string userName = ConfigurationManager.AppSettings["SmtpUserName"];
        private static readonly string password = ConfigurationManager.AppSettings["SmtpPassword"];

        public static bool Send(string toEmail, string subject, string textMail, bool isHtml, List<Attachment> att)
        {
            bool result = false;

            try
            {
                using (MailMessage mm = new MailMessage(from, toEmail))
                {
                    mm.Subject = subject;
                    mm.Body = textMail;
                    mm.IsBodyHtml = isHtml;
                    if (att != null)
                        foreach(Attachment a in att)
                            mm.Attachments.Add(a);

                    using (SmtpClient sc = new SmtpClient(host, int.Parse(port)))
                    {
                        sc.EnableSsl = true;
                        sc.UseDefaultCredentials = false;
                        sc.DeliveryMethod = SmtpDeliveryMethod.Network;
                        sc.Credentials = new NetworkCredential(userName, password);
                        sc.Send(mm);
                        result = true;
                    }
                }
                
                return result;
            }
            catch (Exception e)
            {
                
            }

            return result;
        }
    }
}
